#!/bin/bash

today=`date +"%Y-%m-%d"`
def_begin=2015-12-29
def_end=2016-01-01
def_save_dir=/home/wzq/tmp/es2/$today
# def_begin=2015-12-03
# def_end=`date +"%Y-%m-%d" -d last-day`
# def_save_dir=/data/backup/es/$today

read -e -p "begin date  : " -i $def_begin		begin
read -e -p "end date    : " -i $def_end      	end
read -e -p "save folder : " -i $def_save_dir 	save_dir
echo "begin="$begin 
echo "end="$end 
echo "save_dir="$save_dir

if [ ! -f $save_dir ]; then
	mkdir -p $save_dir
fi


es_url=http://localhost:9200
index_pre=logstash_api-
function backOneIndex {
	index=$index_pre$1
	url=$es_url/$index
	output=$save_dir/$index.json
	echo "********************************"
	echo "index  : "$index
	echo "url    : "$url
	echo "output : "$output
	elasticdump --input=$url --output=$output --type=data 
	# >> /dev/null
}

tmpday=`date +%Y%m%d -d $begin`
lastday=`date +%Y%m%d -d $end`
while [ "$lastday" -ge $tmpday ]; do
    # echo $tmpday
    backOneIndex $tmpday
    tmpday=`date +%Y%m%d -d "$tmpday +1 days"`
done

# do scp
# scp -r admin.yueniapp:$save_dir $save_dir